<?php 
defined('RUN_KATANA') or die('Restricted access');
class OrderItem extends Object
{
	public $OrderId;
	public $ProductId;
	public $VariationId;
	public $CatId;
	public $GroupId;
	public $TaxValue;
	public $Count;
	public $CountDelivered;
	
	public $ItemTitle;
	public $ItemVarTitle;
	public $ItemIdentity;
	public $ItemExtIdentity;
	
	public $SingleItemPrice;
	public $SingleItemPriceNow;
	public $SingleItemTax;
	public $SingleItemPriceTotal;
	
	public $TotalItemPrice;
	public $TotalItemTax;
	public $TotalItemPriceTotal;
	
	public $SingleItemVolume;
	public $SingleItemNetWeight;
	public $SingleItemGrosWeight;
	
	public $TotalItemVolume;
	public $TotalItemNetWeight;
	public $TotalItemGrosWeight;
	
	public $ItemDimUnit;
	public $ItemWeightUnit;
	
	public $InsertReady;
	public $Valid;
	
	public $ItemText;
	public $ItemImage;
	
	public function OrderItem() 
	{
		$this->CountDelivered = 0;
		$this->Count = 0;
	}
	
	// Get Methods
	public function getOrderId() { return intval($this->OrderId); }
	public function getProductId() { return intval($this->ProductId); }
	public function getVariationId() { return intval($this->VariationId); }
	public function getCatId() { return intval($this->CatId); }
	public function getGroupId() { return intval($this->GroupId); }
	public function getTaxValue() { return $this->TaxValue; }
	public function getCount() { return intval($this->Count); }
	public function getCountDelivered() { return intval($this->CountDelivered); }
	
	public function getSingleItemPrice($round = 0) 
	{
		if($round > 0)
		{ return round($this->SingleItemPrice, $round); }
		else
		{ return $this->SingleItemPrice; }
	}
	public function getSingleItemTax($round = 0) 
	{
		if($round > 0)
		{ return round($this->SingleItemTax, $round); }
		else
		{ return $this->SingleItemTax; }
	}
	public function getSingleItemPriceTotal($round = 0)
	{
		if($round > 0)
		{ return round($this->SingleItemPriceTotal, $round); }
		else
		{ return $this->SingleItemPriceTotal; }
	}
	
	public function getTotalItemPrice($round = 0)
	{
		if($round > 0)
		{ return round($this->TotalItemPrice, $round); }
		else
		{ return $this->TotalItemPrice; }
	}
	public function getTotalItemTax($round = 0)
	{
		if($round > 0)
		{ return round($this->TotalItemTax, $round); }
		else
		{ return $this->TotalItemTax; }
	}
	public function getTotalItemPriceTotal($round = 0)
	{
		if($round > 0)
		{ return round($this->TotalItemPriceTotal, $round); }
		else
		{ return $this->TotalItemPriceTotal; }
	}
	
	public function getSingleItemVolume() { return intval($this->SingleItemVolume); }
	public function getSingleItemNetWeight() { return intval($this->SingleItemNetWeight); }
	public function getSingleItemGrosWeight() { return intval($this->SingleItemGrosWeight); }
	
	public function getTotalItemVolume() { return intval($this->SingleItemVolume); }
	public function getTotalItemNetWeight() { return intval($this->TotalItemNetWeight); }
	public function getTotalItemGrosWeight() { return intval($this->TotalItemGrosWeight); }
	
	public function getItemDimUnit() { return $this->ItemDimUnit; }
	public function getItemWeightUnit() { return $this->ItemWeightUnit; }
	
	public function getInsertReady() { return $this->InsertReady; }
	public function getValid() { return $this->Valid; }
	
	public function getItemText() { return $this->ItemText; }
	public function getItemImage() { return $this->ItemImage; }
	public function getItemImageUrl($baseUrl='', $index=0) 
	{ 
		if($index == 0) //largest image
		{ return $baseUrl.$this->ItemImage; }
		if($index == 1) //largest thumb
		{ return $baseUrl.'thumbs/'.$this->ItemImage; }
		if($index > 1) //largest thumb
		{ 
			global $thumb;
			return $baseUrl.'thumbs/'.$thumb['CART'][$index]['MAXW'].'_'.$this->ItemImage; 
		}
	}
	
	// Set Methods
	public function setOrderId($val) { $this->OrderId=intval($val); }
	public function setProductId($val) { $this->ProductId=intval($val); }
	public function setVariationId($val) { $this->VariationId=intval($val); }
	public function setCatId($val) { $this->CatId = intval($val); }
	public function setGroupId($val) { $this->GroupId = intval($val); }
	public function setTaxValue($val) { $this->TaxValue=$val; }
	public function setCount($val) { $this->Count=intval($val); }
	public function setCountDelivered($val) { $this->CountDelivered=intval($val); }
	
	public function setSingleItemPrice($val) { $this->SingleItemPrice=$val; }
	public function setSingleItemPriceNow($val) { $this->SingleItemPriceNow=$val; }
	public function setSingleItemTax($val) { $this->SingleItemTax=$val; }
	public function setSingleItemPriceTotal($val) { $this->SingleItemPriceTotal=$val; }
	
	public function setTotalItemPrice($val) { $this->TotalItemPrice=$val; }
	public function setTotalItemTax($val) { $this->TotalItemTax=$val; }
	public function setTotalItemPriceTotal($val) { $this->TotalItemPriceTotal=$val; }
	
	public function setSingleItemVolume($val) { $this->SingleItemVolume=intval($val); }
	public function setSingleItemNetWeight($val) { $this->SingleItemNetWeight=intval($val); }
	public function setSingleItemGrosWeight($val) { $this->SingleItemGrosWeight=intval($val); }
	
	public function setTotalItemVolume($val) { $this->TotalItemVolume=intval($val); }
	public function setTotalItemNetWeight($val) { $this->TotalItemNetWeight=intval($val); }
	public function setTotalItemGrosWeight($val) { $this->TotalItemGrosWeight=intval($val); }
	
	public function setInsertReady($val) { $this->InsertReady=$val; }
	public function setValid($val) { $this->Valid=$val; }
	
	public function setItemText($val) { $this->ItemText=$val; }
	public function setItemImage($val) { $this->ItemImage=$val; }
	
	public function calcTotalValues()
	{
		if($this->Count > 0)
		{
			if($this->SingleItemPrice > 0)
			{
				if(!($this->TaxValue > 0))
				{ $this->TaxValue=1; }
				
				$this->SingleItemPriceTotal = ($this->TaxValue * $this->SingleItemPrice);
				$this->SingleItemTax = (($this->SingleItemPriceTotal) - $this->SingleItemPrice);
				
				$this->TotalItemPrice = ($this->SingleItemPrice * $this->Count);
				$this->TotalItemTax = ($this->SingleItemTax * $this->Count);
				$this->TotalItemPriceTotal = ($this->SingleItemPriceTotal * $this->Count);
				self::setValid(true);
			}
			
			if($this->SingleItemVolume > 0)
			{ $this->TotalItemVolume = $this->SingleItemVolume*$this->Count; }
			else
			{ $this->TotalItemVolume = 0; }
			
			if($this->SingleItemNetWeight > 0)
			{ $this->TotalItemNetWeight = $this->SingleItemNetWeight*$this->Count; }
			else
			{ $this->TotalItemNetWeight = 0; }
			
			if($this->SingleItemGrosWeight > 0)
			{ $this->TotalItemGrosWeight = $this->SingleItemGrosWeight*$this->Count; }
			else
			{ $this->TotalItemGrosWeight = 0; }
		}
	}
	
	public function insertOrderItem()
	{
		self::calcTotalValues();
		if((self::getValid()) && (self::getOrderId() > 0))
		{ 	
			$sql  = 'INSERT INTO '.DB_PERFIX.'shop_order_items';
			$sql .= ' (';
			$sql .= ' `order_id`, `product_id`, `variation_id`, `CatId`, `GroupId`, `tax_value`, `count_ordered`, `count_delivered`, `item_title`, `var_title`, `item_identity`, ';
			$sql .= ' `item_identity_ext`, `single_item_price`, `single_item_tax`, `single_item_price_total`, `single_item_volume`, ';
			$sql .= ' `single_item_net_weight`, `single_item_gros_weight`, `item_text`, `item_image`';
			$sql .= ' )';
			$sql .= ' VALUES ';
			$sql .= ' (';
			$sql .= ' '.self::getOrderId().', ';
			$sql .= ' '.self::getProductId().', ';
			$sql .= ' '.self::getVariationId().', ';
			$sql .= ' '.self::getCatId().', ';
			$sql .= ' '.self::getGroupId().', ';
			$sql .= ' '.self::getTaxValue().', ';
			$sql .= ' '.self::getCount().', ';
			$sql .= ' '.self::getCountDelivered().', ';
			$sql .= '"'.self::getItemTitle().'", ';
			$sql .= '"'.self::getItemVarTitle().'", ';
			$sql .= '"'.self::getItemIdentity().'", ';
			$sql .= '"'.self::getItemExtIdentity().'", ';
			$sql .= ' '.self::getSingleItemPrice(2).', ';
			$sql .= ' '.self::getSingleItemTax(2).', ';
			$sql .= ' '.self::getSingleItemPriceTotal(2).', ';
			$sql .= ' '.self::getSingleItemVolume().', ';
			$sql .= ' '.self::getSingleItemNetWeight().', ';
			$sql .= ' '.self::getSingleItemGrosWeight().', ';
			$sql .= '"'.self::getItemText().'", ';
			$sql .= '"'.self::getItemImage().'" ';
			$sql .= ' );';
			$result = mysql_query($sql);
			
			$select = 'SELECT id FROM '.DB_PERFIX.'shop_order_items WHERE order_id='.self::getOrderId().' AND product_id='.self::getProductId().' ORDER BY id DESC LIMIT 1;';
			$result = mysql_query($select);
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{ self::setId($row['id']); }
			}
		}
	}
	
	public function moveImage($oldPath = '', $newPath = '')
	{
		if(($oldPath == '') && ($newPath == ''))
		{ 
			$oldPath = CART_PATH; 
			$newPath = ORDER_TEMP_PATH;
		}
		if((self::getItemImage() != '') && (self::getId() > 0))
		{
			if(is_file($oldPath.self::getItemImage()))
			{ 
				$imageClass = new Image();
				$imageClass->moveRenameImageWithThumbs($oldPath, $newPath, self::getItemImage(), self::getItemImage(), 'CART');
			}
		}
	}
	
	public function uploadImage($dateDelivery = '')
	{
		global $shop;
		global $txt;
		$out['info'] = '';
		$out['error'] = '';
		if((is_file(ORDER_IMAGES_PATH.self::getItemImage())) && ($shop->getGlobalSetting('ftp_upload_order_images') > 0))
		{
			$info = '<br />ftp_connect('.$shop->getGlobalSetting('ftp_server').', '.$shop->getGlobalSetting('ftp_port').')';
			$conn = ftp_connect($shop->getGlobalSetting('ftp_server'), $shop->getGlobalSetting('ftp_port'));
			if($conn)
			{
				$out['info'] .= 'Connected to server.<br />';
				$login = ftp_login($conn, $shop->getGlobalSetting('ftp_username'), $shop->getGlobalSetting('ftp_password')) or die("Cannot login");
				if($login)
				{
					$week = 0;
					if($dateDelivery != '')
					{ $week = self::formatDateTime($dateDelivery, 'W'); }
					if($week > 0)
					{ $targetDIR = date('Y').'-Uke-'.$week; }
					else
					{ $targetDIR = date('Y').'-Uke-'.date('W'); }
					if(!ftp_chdir($conn, $targetDIR))
					{ $dirinfo = ftp_mkdir($conn, $targetDIR); }
					else
					{ ftp_chdir($conn, '/'); }
					$out['info'] .= $dirinfo;
					$targetFile = '/'.$targetDIR.'/'.self::getItemImage();
					$upload = ftp_put($conn, $targetFile, ORDER_IMAGES_PATH.self::getItemImage(), FTP_BINARY);
					if(!$upload)
					{ $out['error'] .= 'Upload failed.<br />'; }
					else
					{ $out['info'] .= 'File uploaded.<br />'; }
				}
				else
				{ $out['error'] .= 'Could not login to server.<br />'; }
				
				ftp_close($conn);
			}
			else
			{ $out['error'] .= 'Could not connect to server.<br />'; }
		}
	}
}
?>